﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using ClassLibraryEntidades;
using System.Xml.Serialization;
using System.IO;

namespace ClassLibraryServer.Negocio
{
    public class Util
    {
        /// <summary>
        /// Deja una calle con el puro nombre de la misma
        /// </summary>
        /// <param name="calle"></param>
        /// <returns></returns>
        public static string FormatearCalleSinQuitarAcentos(string calle)
        {
            calle = calle.ToLower();
            calle = calle.Replace(" ", String.Empty);
            calle = calle.Replace(".", String.Empty);
            calle = calle.Replace(",", String.Empty);
            calle = calle.Replace("calle", String.Empty);
            calle = calle.Replace("c/", String.Empty);
            calle = calle.Replace(@"c\", String.Empty);
            calle = calle.Replace("avenida", String.Empty);
            calle = calle.Replace("avda", String.Empty);
            calle = calle.Replace("av", String.Empty);
            calle = calle.Replace("carretera", String.Empty);
            calle = calle.Replace("crt", String.Empty);
            calle = calle.Replace("poligono", String.Empty);


            return calle;

        }

        public static string FormatearCalleQuitandoAcentos(string calle)
        {
            calle = calle.ToLower();
            calle = calle.Replace(" ", String.Empty);
            calle = calle.Replace(".", String.Empty);
            calle = calle.Replace(",", String.Empty);
            calle = calle.Replace("calle", String.Empty);
            calle = calle.Replace("c/", String.Empty);
            calle = calle.Replace(@"c\", String.Empty);
            calle = calle.Replace("avenida", String.Empty);
            calle = calle.Replace("avda", String.Empty);
            calle = calle.Replace("av", String.Empty);
            calle = calle.Replace("carretera", String.Empty);
            calle = calle.Replace("crt", String.Empty);
            calle = calle.Replace("poligono", String.Empty);

            //------------Eliminación de acentos-------------------
            String normalizedString = calle.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < normalizedString.Length; i++)
            {
                Char c = normalizedString[i];

                if (Char.IsPunctuation(c) || Char.IsSymbol(c) || char.IsNumber(c))
                    return null;

                if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                    stringBuilder.Append(c);

            }

            calle = stringBuilder.ToString();
            //-------------------------------------------------------

            return calle;

        }

        public static Dictionary<string, Ruta> ObtenerRutasDesdeArchivo(string pathFile)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(RutasPool));
            StreamReader sr = new StreamReader(pathFile);
            RutasPool rutasPool=(RutasPool)xmlSerializer.Deserialize(sr);
            sr.Close();

            Dictionary<string, Ruta> result = new Dictionary<string, Ruta>(rutasPool.rutas.Count);
            foreach (Ruta ruta in rutasPool.rutas)
            {
                result.Add(ruta.IDRuta, ruta);
            }
            return result;

        }

        public static void GuardaRutasEnArchivo(string pathFile,Dictionary<string, Ruta> tablaRutas)
        {
            
            RutasPool rutasPool = new RutasPool();

            foreach (KeyValuePair<string,Ruta> keyRuta_ruta in tablaRutas)
            {
                keyRuta_ruta.Value.IDRuta = keyRuta_ruta.Key;
                rutasPool.rutas.Add(keyRuta_ruta.Value);
            }

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(RutasPool));
            StreamWriter sw=new StreamWriter(pathFile);
            xmlSerializer.Serialize(sw,rutasPool);
            sw.Close();
        }
    }

    public class RutasPool
    {
        public List<Ruta> rutas;

        public RutasPool()
        {
            this.rutas=new List<Ruta>();
        }
    }
}
